package com.game.screen;

import java.util.ArrayList;
import java.util.List;

import com.game.GameJam;
import com.game.screen.physics.HorizontalPlayerPhysics;
import com.game.screen.physics.PlayerPhysics;

import processing.core.PApplet;

public class Player extends Sprite {

	public static int numBubbles = 2;
	public static boolean asleep = true;
	protected float speed;
	
//take out
	private int moveSpeed = 3;
	private int maxY = 200;
	private int maxX = 250;
	private float windIter = 0.0f;
	private float windForce = 0.5f;
	
	public static Spring2D boy;

	public static final String BLUE = "bluebubble.png";
	public static final String RED = "redbubble.png";
	public static final String GREEN = "greenbubble.png";
	public static final String YELLOW = "yellowbubble.png";
	public static final String EEGG1 = "cloudbubble.png";
	public static final String EEGG2 = "E2";

	public static String nextBub = null;
	
	public static List<Spring2D> bubbles = new ArrayList<Spring2D>();

	float gravity = 6.0f;
	float mass = 2.0f;

	private PlayerPhysics playerPhysics = new HorizontalPlayerPhysics();
	
	public Player(String img, int x, int y) {
		super(img, x, y);
		speed = 1;
		
		for(int i = 0; i < 15; i++){
			bubbles.add(new Spring2D("cloudbubble.png", x, y, mass, gravity));
		}
		
		boy = new Spring2D("boyL2.png", x, y, mass * 2, gravity);
		
//		bubbles.add(boy);
	}

	public void update(){
		playerPhysics.update(this);
	}
	
	public void display() {
		super.display();
//		windIter += 0.05f;

//		position.setX(position.getX() - (float)(Math.sin(windIter) * windForce));
//		position.setY(position.getY() - (float)(Math.cos(windIter) * windForce));
		
		if (asleep) {

//			if(nextBub != null){
//				bubbles.get(numBubbles - 1).setImage(nextBub);
//				nextBub = null;
//			}
//			
//			float newX = pApplet.lerp(pApplet.mouseX, position.getX(), 0.99f);
//
//			if(newX < maxX)
//				position.setX(newX - (float) (Math.sin(windIter) * windForce));
//			position.setY(position.getY()
//					- (float) (Math.cos(windIter) * windForce));
//
//			if (isRising) {
//				position.setY(position.getY() - riseSpeed);
//			} else {
//				((GameScreen) ((GameJam) pApplet).currentScreen).bg.pause = false;
//			}
//
//			isRising = (position.getY() <= maxY) ? false : true;
//
//			bubbles.get(0).update(position.getX() + 25, position.getY() + 35);
			bubbles.get(0).display();

			for (int i = 1; i < numBubbles; i++) {
				bubbles.get(i).display();
			}
			
			boy.display();
		} 
	}

	public void onKeyPressed(char key, int keyCode){
		
		switch (keyCode) {
		case PApplet.LEFT:
			if(position.getX() > 15)
				position.setX(position.getX() - moveSpeed);
			break;
		case PApplet.RIGHT:
			if(position.getX() < 225)
				position.setX(position.getX() + moveSpeed);
			break;
		case PApplet.UP:
		//	position.setY(position.getY() - riseSpeed);
			break;
		case PApplet.DOWN:
			//position.setY(position.getY() + riseSpeed);
			break;
		default:
			break;
		}
	}
	

	public class Spring2D extends Sprite{
	  float vx, vy; // The x- and y-axis velocities
	  public float x, y; // The x- and y-coordinates
	  float gravity;
	  float mass;
	  float radius = 20;
	  float stiffness = 0.7f;
	  float damping = 0.25f;
	  boolean isBoy = false;
	  
	  Spring2D(String img, float xpos, float ypos, float m, float g) {
		  super(img, 0,0);
	    x = xpos;
	    y = ypos;
	    mass = m;
	    gravity = g;
	  }
	  
	  public void update(float targetX, float targetY) {
	    float forceX = (targetX - x) * stiffness;
	    float ax = forceX / mass;
	    vx = damping * (vx + ax);
	    x += vx;
	    float forceY = (targetY - y) * stiffness;
	    forceY += gravity;
	    float ay = forceY / mass;
	    vy = damping * (vy + ay);
	    y += vy;
	  }
	  
	  public void setImage(String img){
			pimg = pApplet.loadImage(img);
	  }
	  
	  public void display() {
		  position.set((int)x, (int)y);
		  pApplet.image(pimg, x, y);
	  }
	
}
}
